package com.kettle.controller;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kettle.controllerapis.QuartzControllerApi;
import com.kettle.model.quartz.JobPost;
import com.kettle.model.quartz.ReturnResult;
import com.kettle.model.rowpage.PageResult;
import com.kettle.service.quart.QuartzService;

import javax.annotation.Resource;

/**
 * 定时任务 Controller
 * @author：king
 * @date：2019年7月5日 
 */
@Controller
@RequestMapping(value = "/quartz")
public class QuartzController implements QuartzControllerApi{

    @Resource
    private QuartzService quartzService;
    
    /**
             * 定时列表页
     * @return
     * @throws SchedulerException
     */
    @Override
    @RequestMapping(value = "/listJob",method = RequestMethod.POST)
    public @ResponseBody PageResult listJob(Integer page, Integer rows){
        PageResult result=new PageResult();;
		try {
			result = quartzService.getSchedulerJobInfo();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
        return result;
    }

    /**
     * 新建job
     * @param jobDto
     * @return
     */
    @Override
    @RequestMapping(value = "/addJob",method = RequestMethod.POST)
    public @ResponseBody ReturnResult save(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
    	try {
			retval=quartzService.addJob(jobDto.getJobName(), jobDto.getJobGroupName(), 
			    		jobDto.getTriggerName(),jobDto.getTriggerGroupName(), 
			    		Class.forName(jobDto.getJobClass()), jobDto.getCronExpression());
		} catch (ClassNotFoundException e) {
			retval.setCode("500");
			retval.setSuccess(false);
			retval.setMsg("出现异常："+e.getMessage());
			e.printStackTrace();
		}
        return retval;
    }

    /**
     * 编辑job
     * @param jobDto
     * @return
     */
    @Override
    @RequestMapping(value = "/editJob", method = RequestMethod.POST)
    public @ResponseBody ReturnResult edit(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
    	retval = quartzService.modifyJobTime(jobDto.getOldJobName(), jobDto.getOldJobGroupName(), jobDto.getOldTriggerName(), 
    			jobDto.getOldTriggerGroupName(), jobDto.getJobName(), jobDto.getJobGroupName(), jobDto.getTriggerName(), 
    			jobDto.getTriggerGroupName(), jobDto.getCronExpression());
        return retval;
    }
    
    @Override
    @RequestMapping(value = "/editJobtime", method = RequestMethod.POST)
    public @ResponseBody ReturnResult edittime(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
    	retval = quartzService.modifyJobTime(jobDto.getJobName(), jobDto.getJobGroupName(),jobDto.getCronExpression());
        return retval;
    }

    /**
     * 暂停job
     * @param jobName
     * @param jobGroupName
     * @return
     */
    @Override
    @RequestMapping(value = "/stopJob", method = RequestMethod.POST)
    public @ResponseBody ReturnResult stopJob(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
        if (jobDto.getJobName()==null || jobDto.getJobGroupName()==null) {
        	retval.setCode("500");
        	retval.setSuccess(false);
        	retval.setMsg("工作名称和工作组名不能为空");
        } else {
        	retval=quartzService.pauseJob(jobDto.getJobName(), jobDto.getJobGroupName());
        }
        return retval;
    }

    /**
     * 恢复job
     * @param jobName
     * @param jobGroupName
     * @return
     */
    @Override
    @RequestMapping(value = "/resumeJob", method = RequestMethod.POST)
    public @ResponseBody ReturnResult resumeJob(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
    	 if (jobDto.getJobName()==null || jobDto.getJobGroupName()==null) {
         	retval.setCode("500");
         	retval.setSuccess(false);
         	retval.setMsg("工作名称和工作组名不能为空");
         } else {
        	retval=quartzService.resumeJob(jobDto.getJobName(), jobDto.getJobGroupName());
         }
        return retval;
    }

    /**
     * 删除job
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteJob", method = RequestMethod.DELETE)
    public @ResponseBody ReturnResult deleteJob(JobPost jobDto) {
    	ReturnResult retval = ReturnResult.newInstance();
        if (jobDto.getJobName()==null || jobDto.getJobGroupName()==null 
        		|| jobDto.getTriggerName()==null || jobDto.getTriggerGroupName()==null) {
        	retval.setCode("500");
         	retval.setSuccess(false);
         	retval.setMsg("工作名称和工作组名及触发器名和触发组名不能为空");
        }else{
        	retval=quartzService.removeJob(jobDto.getJobName(), jobDto.getJobGroupName(), jobDto.getTriggerName(),jobDto.getTriggerGroupName());
        }
        return retval;
    }
    /**
     * 开启全部任务
     * @return
     */
    @Override
    @RequestMapping(value = "/startSchedule", method = RequestMethod.POST)
    public @ResponseBody ReturnResult startSchedule(){
    	ReturnResult retval = ReturnResult.newInstance();
    	retval=quartzService.startSchedule();
    	return retval;
    }
    
    /**
     * 停止全部任务
     * @return
     */
    @Override
    @RequestMapping(value = "/shutdownSchedule", method = RequestMethod.POST)
    public @ResponseBody ReturnResult shutdownSchedule(){
    	ReturnResult retval = ReturnResult.newInstance();
    	retval=quartzService.shutdownSchedule();
    	return retval;
    }
}
